package com.n3wax.automation.security;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.n3wax.automation.security.AlarmConstants;
import com.n3wax.automation.security.ademco.AlarmSystemStatus;
import com.n3wax.automation.security.ademco.ad2usb.AD2USBController;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

import java.io.IOException;
import java.lang.reflect.Field;

/**
 * Created by IntelliJ IDEA.
 * User: HP_Administrator
 * Date: 11/9/11
 * Time: 10:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class WebAlarmHandler extends AbstractHandler implements AlarmConstants {

    AD2USBController control;

    public WebAlarmHandler(AD2USBController controller) {
        control = controller;
    }

    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);

        String command = (String) baseRequest.getParameter("command");

        response.getWriter().println("<html><head><title>30 Merion Alarm</title></head><font face=\"Arial\">");

        if (command == null) {
            switch (control.getSystemStatus()) {
                case _READY:
                    response.getWriter().println("<font color = green><b>READY TO ARM</b></font>");
                    break;
                case _ARMED_AWAY:
                    response.getWriter().println("<font color = red><b>ARMED AWAY MODE</b></font>");
                    break;
                case _ARMED_STAY:
                    response.getWriter().println("<font color = red><b>ARMED STAY MODE</b></font>");
                    break;
                case _ARMED_INSTANT:
                    response.getWriter().println("<font color = red><b>ARMED INSTANT MODE</b></font>");
                    break;
                case _NOT_READY:
                    response.getWriter().println("<B>NOT READY: ZONE FAULTS</B>");
                    break;
            }
            response.getWriter().println("<br>");
            AlarmSystemStatus statusObj = control.getSystemStatusDetail();

            try {
                Class statusClass = Class.forName("com.n3wax.automation.security.ademco.AlarmSystemStatus");

                for (int cnt = 1; cnt < 16; cnt++) {
                    String zoneTimestampIdentifier;
                    String zoneIdentifier;
                    if (cnt < 10) {

                        zoneIdentifier = "zone0" + cnt + "fault";
                    } else {

                        zoneIdentifier = "zone" + cnt + "fault";
                    }

                    Field zoneFault = statusClass.getDeclaredField(zoneIdentifier);

                    zoneFault.setAccessible(true);

                    if(zoneFault.getBoolean(statusObj))
                    {
                        response.getWriter().println("<font color=\"red\">Zone " + cnt + " fault</font><br><br>");
                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            response.getWriter().println("<a href=\"/?command=disarm\">Disarm<br></a>");
            response.getWriter().println("<a href=\"/?command=armaway\">Arm Away<br></a>");
            response.getWriter().println("<a href=\"/?command=armstay\">Arm Stay<br></a>");
            response.getWriter().println("<a href=\"/?command=bypass\">Auto Bypass<br></a>");

        } else {
            if (command.equalsIgnoreCase("armstay")) {
                control.arm(AlarmConstants._ARMED_STAY, "4616", true);
                response.getWriter().println("Alarm ARMING (Stay)");
            }

            if (command.equalsIgnoreCase("armaway")) {
                control.arm(AlarmConstants._ARMED_AWAY, "4616", true);
                response.getWriter().println("Alarm ARMING (Away)");
            }

            if (command.equalsIgnoreCase("disarm")) {
                control.disarm("4616");
                response.getWriter().println("Alarm DISARMING");
            }

            if (command.equalsIgnoreCase("bypass")) {
                response.getWriter().println("Now bypassing...please wait!<br>");
                response.flushBuffer();
                control.autoBypass("4616");
                response.getWriter().println("Zones Bypassed");
            }

            response.getWriter().println("<a href=\"/\"><br>Return to status<br></a>");

            System.out.println("Request Handled");
        }
    }
}
